package com.linkage.educloud.ah.view.activity.topic;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.topic.TopicChildViewPagerAdapter;
import com.linkage.educloud.ah.bean.LabelBean;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.evenbus.TopicEvent;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicChildContract;
import com.linkage.educloud.ah.mvp.presenter.topic.TopicChildPresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.view.fragment.topic.TopicChildListFragment;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/11/6.
 */

public class TopicChildListActivity extends BasicTitleActivity implements ITopicChildContract.IView, View.OnClickListener {

    @BindView(R.id.ac_topic_child_tabs)
    TabLayout ac_topic_child_tabs;
    @BindView(R.id.ac_topic_child_sort)
    LinearLayout ac_topic_child_sort;
    @BindView(R.id.ac_topic_child_sort_text)
    TextView ac_topic_child_sort_text;
    @BindView(R.id.ac_topic_child_sort_img)
    ImageView ac_topic_child_sort_img;
    @BindView(R.id.ac_topic_child_viewpager)
    ViewPager ac_topic_child_viewpager;
    @BindView(R.id.ac_topic_child_write)
    LinearLayout ac_topic_child_write;
    @BindView(R.id.ac_topic_child_pop)
    RelativeLayout ac_topic_child_pop;
    @BindView(R.id.ac_topic_child_release)
    TextView ac_topic_child_release;
    @BindView(R.id.ac_topic_child_reply)
    TextView ac_topic_child_reply;
    @BindView(R.id.ac_topic_child_dark_bg)
    View ac_topic_child_dark_bg;

    ITopicChildContract.IPresenter iPresenter;
    TopicChildViewPagerAdapter adapter;
    List<LabelBean> labelBeans = new ArrayList<>();
    List<TopicChildListFragment> fragmentList = new ArrayList<>();
    int sort = 1;//排序方式默认为第一个

    public static String KEY_TITLE = "title";
    public static String KEY_ID = "id";

    public static void launch(Context context, String title, String id) {
        Intent intent = new Intent(context, TopicChildListActivity.class);
        intent.putExtra(KEY_TITLE, title);
        intent.putExtra(KEY_ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_topicchild_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        iPresenter = new TopicChildPresenter(this, this);
        iPresenter.getLabels(getIntent().getStringExtra(KEY_ID));
    }

    private void initView() {
        setTitle(getIntent().getStringExtra(KEY_TITLE));
        ac_topic_child_sort.setOnClickListener(this);
        ac_topic_child_write.setOnClickListener(this);
        ac_topic_child_release.setOnClickListener(this);
        ac_topic_child_reply.setOnClickListener(this);
        ac_topic_child_dark_bg.setOnClickListener(this);

        LinearLayout linearLayout = (LinearLayout) ac_topic_child_tabs.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.color.pop_rest_bg));
        linearLayout.setDividerPadding(8);

        ac_topic_child_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                ac_topic_child_pop.setVisibility(View.GONE);
                if (labelBeans.get(arg0).getIs_show().equals("1")) {
                    ac_topic_child_release.setBackgroundResource(R.color.text_gray_bg);
                    ac_topic_child_reply.setBackgroundResource(R.color.white);
                } else {
                    ac_topic_child_release.setBackgroundResource(R.color.white);
                    ac_topic_child_reply.setBackgroundResource(R.color.text_gray_bg);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    public void initLabelDate(List<LabelBean> list) {
        labelBeans = list;
        for (int a = 0; a < labelBeans.size(); a++) {
            ac_topic_child_tabs.addTab(ac_topic_child_tabs.newTab().setText(labelBeans.get(a).getName()).setTag(labelBeans.get(a).getId()), a);
            fragmentList.add(TopicChildListFragment.newInstance(labelBeans.get(a).getName(), String.valueOf(sort), getIntent().getStringExtra(KEY_ID)));
        }
        adapter = new TopicChildViewPagerAdapter(getSupportFragmentManager(), fragmentList, list);
        ac_topic_child_viewpager.setAdapter(adapter);
        ac_topic_child_viewpager.setCurrentItem(0);
        ac_topic_child_tabs.setupWithViewPager(ac_topic_child_viewpager, true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_topic_child_sort:
                if (ac_topic_child_pop.getVisibility() == View.VISIBLE) {
                    hidePop(true);
                } else {
                    hidePop(false);
                }
                break;
            case R.id.ac_topic_child_write:
                TopicWriterActivity.launch(this, getIntent().getStringExtra(KEY_ID));
                break;
            case R.id.ac_topic_child_release:
                changeSort(true);
                break;
            case R.id.ac_topic_child_reply:
                changeSort(false);
                break;
            case R.id.ac_topic_child_dark_bg:
                hidePop(true);
                break;
        }
    }

    private void changeSort(boolean is_change) {
        if (is_change) {
            ac_topic_child_release.setBackgroundResource(R.color.text_gray_bg);
            ac_topic_child_reply.setBackgroundResource(R.color.white);
            int index1 = ac_topic_child_viewpager.getCurrentItem();
            if (labelBeans.size() > 1) {
                labelBeans.get(index1).setIs_show("1");
            }
            sort = 1;
            hidePop(true);
            EventBus.getDefault().post(new TopicEvent(sort));
        } else {
            ac_topic_child_release.setBackgroundResource(R.color.white);
            ac_topic_child_reply.setBackgroundResource(R.color.text_gray_bg);
            int index2 = ac_topic_child_viewpager.getCurrentItem();
            if (labelBeans.size() > 1) {
                labelBeans.get(index2).setIs_show("2");
            }
            sort = 2;
            hidePop(true);
            EventBus.getDefault().post(new TopicEvent(sort));
        }
    }

    private void hidePop(boolean is_hide) {
        if (is_hide) {
            ac_topic_child_pop.setVisibility(View.GONE);
            ac_topic_child_sort_img.setImageResource(R.mipmap.ic_small_down);
            ac_topic_child_sort_text.setTextColor(getResources().getColor(R.color.dark_gray));
        } else {
            ac_topic_child_pop.setVisibility(View.VISIBLE);
            ac_topic_child_sort_img.setImageResource(R.mipmap.ic_small_up);
            ac_topic_child_sort_text.setTextColor(getResources().getColor(R.color.topic_bg));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_topic_child, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelectedCompat(MenuItem item) {
        int i = item.getItemId();
        switch (i) {
            case R.id.menu_topic_search:
                TopicSearchActivity.launch(this,getIntent().getStringExtra(KEY_ID),adapter.getPageTitle(ac_topic_child_viewpager.getCurrentItem()));
                break;
            case R.id.menu_my_topic:
                TopicMineActivity.launch(this);
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (Constants.MENU_BUILDER.equalsIgnoreCase(menu.getClass().getSimpleName())) {
                try {
                    @SuppressLint("PrivateApi")
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    //无用 和fragment共有一个contract的
    @Override
    public void initRecylerviewDate(List<TopicChildBean> list) {

    }

    //无用 和fragment共有一个contract的
    @Override
    public void stopRefresh() {

    }
}
